layui.use(['table', 'admin', 'ax', 'func','laydate','formSelects', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 社区消息管理
     */
    var NanSellAnnounceMsg = {
        tableId: "nanSellAnnounceMsgTable"
    };

    var enumAjax = new $ax(Feng.ctxPath + "/enums/announceMsgType");
    var enumRes = enumAjax.start();
    var projects = enumRes.data;
    var html = "";
    for(var key in projects){
        html += '<option value="'+key+'">'+projects[key]+'</option>'
    }
    $("#type").append(html);
    form.render('select');


    /**
     * 初始化表格的列
     */
    NanSellAnnounceMsg.initColumn = function () {
        return [[
            {field: 'userId', sort: false, title: '用户ID'},
            {field: 'announceId', sort: false, title: '文章ID'},
            {field: 'announceThumb', sort: false, title: '文章主图', templet: '<div><img src="{{Feng.cdn}}{{d.announceThumb}}" alt="" /></div>'},
            {field: 'content', sort: false, title: '内容'},
            {field: 'typeStr', sort: false, title: '类型'},
            {field: 'fromUserId', sort: false, title: '消息来源者'},
            {field: 'fromUserAvatar', sort: false, title: '消息来源者头像', templet: '<div><img src="{{Feng.cdn}}{{d.fromUserAvatar}}" alt="" /></div>'},
            {field: 'fromUserNick', sort: false, title: '消息来源者昵称'},
            {field: 'isRead', sort: false, title: '是否已读', templet: '<div>{{d.isRead? "是":"否"}}</div>'},
            {field: 'commentId', sort: false, title: '评论ID'},
            {field: 'isDel', sort: false, title: '是否删除', templet: '<div>{{d.isDel? "是":"否"}}</div>'},
            {field: 'createTime', sort: false, title: '创建时间'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    NanSellAnnounceMsg.search = function () {
        var queryData = {};

        queryData['userId'] = $('#userId').val();
        queryData['type'] = $('#type').val();

        table.reload(NanSellAnnounceMsg.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    NanSellAnnounceMsg.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/nanSellAnnounceMsg/add'
    };

    /**
    * 跳转到编辑页面
    *
    * @param data 点击按钮时候的行数据
    */
    NanSellAnnounceMsg.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/nanSellAnnounceMsg/edit?id=' + data.id
    };

    /**
     * 导出excel按钮
     */
    NanSellAnnounceMsg.exportExcel = function () {
        var checkRows = table.checkStatus(NanSellAnnounceMsg.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    NanSellAnnounceMsg.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/nanSellAnnounceMsg/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(NanSellAnnounceMsg.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + NanSellAnnounceMsg.tableId,
        url: Feng.ctxPath + '/nanSellAnnounceMsg/list',
        page: true,
        limit: 20,
        height: "full-158",
        cellMinWidth: 100,
        cols: NanSellAnnounceMsg.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        NanSellAnnounceMsg.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    NanSellAnnounceMsg.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        NanSellAnnounceMsg.exportExcel();
    });

    table.on('sort('+NanSellAnnounceMsg.tableId+')', function(obj){
            table.reload(NanSellAnnounceMsg.tableId, {
                initSort: obj,
                where: {
                    field: obj.field.replace(/([A-Z])/g,"_$1").toLowerCase()
                    ,order: obj.type
                }
            });
        });

    // 工具条点击事件
    table.on('tool(' + NanSellAnnounceMsg.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            NanSellAnnounceMsg.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            NanSellAnnounceMsg.onDeleteItem(data);
        }
    });
});
